from ParseTxt import *
import sys
from untitled import Ui_MainWindow
from PyQt5 import QtWidgets, QtGui
import numpy as np
from scipy import fftpack, signal


def GetAcceleration():
    [x, y, z] = read_file()
    num_datos = len(x)
    X = range(0, num_datos, 1)

    # Scale the signal in g's
    for indice in X:
        x[indice] *= g_scale
        y[indice] *= g_scale
        z[indice] *= g_scale

    # Calculates medium value for each channel.
    vdc_canal_1 = 0
    vdc_canal_2 = 0
    vdc_canal_3 = 0
    for indice in X:
        vdc_canal_1 += x[indice]
        vdc_canal_2 += y[indice]
        vdc_canal_3 += z[indice]
    vdc_canal_1 = vdc_canal_1 / num_datos
    vdc_canal_2 = vdc_canal_2 / num_datos
    vdc_canal_3 = vdc_canal_3 / num_datos

    # Substract DC offset
    for indice in X:
        x[indice] -= vdc_canal_1
        y[indice] -= vdc_canal_2
        z[indice] -= vdc_canal_3
    return [x, y, z, num_datos]


class MyWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(MyWindow, self).__init__()
        self.setupUi(self)

        # 画采样的信号
        [x, y, z, num_datos] = GetAcceleration()
        labelStyle = {'color': '#FFF', 'font-size': '18pt'}

        self.plotwidget_x.setXRange(0, num_datos)
        self.plotwidget_x.plot(x, pen='g')
        pltItem_x = self.plotwidget_x.getPlotItem()
        left_axis_x = pltItem_x.getAxis("left")
        bottom_axis_x = pltItem_x.getAxis("bottom")
        left_axis_x.setLabel('g', units='', **labelStyle)
        bottom_axis_x.setLabel('x轴', units='', **labelStyle)

        self.plotwidget_y.setXRange(0, num_datos)
        self.plotwidget_y.plot(y, pen='g')
        pltItem_y = self.plotwidget_y.getPlotItem()
        left_axis_y = pltItem_y.getAxis("left")
        bottom_axis_y = pltItem_y.getAxis("bottom")
        left_axis_y.setLabel('g', units='', **labelStyle)
        bottom_axis_y.setLabel('y轴', units='', **labelStyle)

        self.plotwidget_z.setXRange(0, num_datos)
        self.plotwidget_z.plot(z, pen='g')
        pltItem_z = self.plotwidget_z.getPlotItem()
        left_axis_z = pltItem_z.getAxis("left")
        bottom_axis_z = pltItem_z.getAxis("bottom")
        left_axis_z.setLabel('g', units='', **labelStyle)
        bottom_axis_z.setLabel('z轴', units='', **labelStyle)

        # 画经过FFT之后的频谱图
        N = len(x)
        T = 1.0 / sample_rate
        w = signal.hann(N, sym=False)  # Hanning window
        yf = fftpack.fft(x*w)/(N/2)
        yf = yf[:int(N/2)]
        # xf = np.linspace(0.0, 1.0/(2.0*T), N//2)
        self.plotwidget_fftx.plot(np.abs(yf), pen='g')
        pltItem_fftx = self.plotwidget_fftx.getPlotItem()
        left_axis_fftx = pltItem_fftx.getAxis("left")
        bottom_axis_fftx = pltItem_fftx.getAxis("bottom")
        left_axis_fftx.setLabel('g', units='', **labelStyle)
        bottom_axis_fftx.setLabel('x轴', units='', **labelStyle)

        yf = fftpack.fft(y * w) / (N / 2)
        yf = yf[:int(N / 2)]
        # xf = np.linspace(0.0, 1.0/(2.0*T), N//2)
        self.plotwidget_ffty.plot(np.abs(yf), pen='g')
        pltItem_ffty = self.plotwidget_ffty.getPlotItem()
        left_axis_ffty = pltItem_ffty.getAxis("left")
        bottom_axis_ffty = pltItem_ffty.getAxis("bottom")
        left_axis_ffty.setLabel('g', units='', **labelStyle)
        bottom_axis_ffty.setLabel('y轴', units='', **labelStyle)

        yf = fftpack.fft(z * w) / (N / 2)
        yf = yf[:int(N / 2)]
        # xf = np.linspace(0.0, 1.0/(2.0*T), N//2)
        self.plotwidget_fftz.plot(np.abs(yf), pen='g')
        pltItem_fftz = self.plotwidget_fftz.getPlotItem()
        left_axis_fftz = pltItem_fftz.getAxis("left")
        bottom_axis_fftz = pltItem_fftz.getAxis("bottom")
        left_axis_fftz.setLabel('g', units='', **labelStyle)
        bottom_axis_fftz.setLabel('y轴', units='', **labelStyle)


if __name__ == '__main__':
    #
    # print(a)
    app = QtWidgets.QApplication(sys.argv)
    myshow = MyWindow()
    myshow.show()
    sys.exit(app.exec_())
